home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / tex / dvi / dvipssrc.zoo / atari / search.c < prev   
C/C++ Source or Header  |  1991-04-27  |  9KB  |  313 lines

  1. /*
  2.  *   The search routine takes a directory list, separated by PATHSEP, and
  3.  *   tries to open a file.  Null directory components indicate current
  4.  *   directory. if the file SUBDIR exists and the file is a font file,
  5.  *   it checks for the file in a subdirectory named the same as the font name.
  6.  *   Returns the open file descriptor if ok, else NULL.
  7.  */
  8.  
  9. /*   Modified for the Atari ST by Tim Gallivan 4/27/91  */
  10.  
  11. #include "structures.h" /* The copyright notice in that file is included too! */
  12. #include <ctype.h>
  13. #ifdef SYSV
  14. #define MAXPATHLEN (256)
  15. #else
  16. #ifdef VMS
  17. #define MAXPATHLEN (256)
  18. #else   /* ~SYSV */
  19. #include <sys/param.h>          /* for MAXPATHLEN */
  20. #endif  /* ~SYSV */
  21. #endif
  22. #ifndef MSDOS
  23. #ifndef VMS
  24. #include <pwd.h>
  25. #endif
  26. #endif
  27. /*
  28.  *
  29.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  30.  */
  31.  
  32. #ifdef DEBUG
  33. extern integer debug_flag;
  34. #endif  /* DEBUG */
  35. extern char *mfmode ;
  36. extern int actualdpi ;
  37.  
  38. FILE *
  39. search(path, file, mode)
  40.         char *path, *file, *mode ;
  41. {
  42.    extern char *getenv(), *newstring() ;
  43.    register char *nam ;                 /* index into fname */
  44.    register FILE *fd ;                  /* file desc of file */
  45.    char fname[MAXPATHLEN] ;             /* to store file name */
  46.    static char *home = 0 ;              /* home is where the heart is */
  47.    if (*file == DIRSEP) {               /* if full path name */
  48.       if ((fd=fopen(file,mode)) != NULL)
  49.          return(fd) ;
  50.       else
  51.          return(NULL) ;
  52.    }
  53.  
  54. #ifdef MSDOS
  55.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  56.       if ((fd=fopen(file,mode)) != NULL)
  57.          return(fd) ;
  58.       else
  59.          return(NULL) ;
  60.    }
  61. #else
  62. #ifdef atarist
  63.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  64.       if ((fd=fopen(file,mode)) != NULL)
  65.          return(fd) ;
  66.       else
  67.          return(NULL) ;
  68.    }
  69. #endif
  70. #endif
  71.  
  72.    do {
  73.       /* copy the current directory into fname */
  74.       nam = fname;
  75.       /* copy till PATHSEP */
  76.       if (*path == '~') {
  77.          char *p = nam ;
  78.          path++ ;
  79. #ifdef atarist
  80.          while (*path && *path != PATHSEP &&
  81.         *path != PATHSEP2 && *path != DIRSEP)
  82. #else
  83.          while (*path && *path != PATHSEP && *path != DIRSEP)
  84. #endif
  85.             *p++ = *path++ ;
  86.          *p = 0 ;
  87.          if (*nam == 0) {
  88.             if (home == 0) {
  89.                if (home = getenv("HOME"))
  90.                   home = newstring(home) ;
  91.                else
  92.                   home = "." ;
  93.             }
  94.             strcpy(fname, home) ;
  95.          } else {
  96. #ifdef MSDOS
  97.             error("! ~username in path???") ;
  98. #else
  99. #ifdef atarist
  100.             error("! ~username in path???") ;
  101. #else
  102. #ifdef VMS
  103.             error("! ~username in path???") ;
  104. #else
  105.             struct passwd *pw = getpwnam(fname) ;
  106.             if (pw)
  107.                strcpy(fname, pw->pw_dir) ;
  108.             else
  109.                error("no such user") ;
  110. #endif
  111. #endif
  112. #endif
  113.          }
  114.          nam = fname + strlen(fname) ;
  115.       }
  116. #ifdef atarist
  117.       while (*path != PATHSEP && *path != PATHSEP2 && *path)
  118. #else
  119.       while (*path != PATHSEP && *path)
  120. #endif
  121.      *nam++ = *path++;
  122.       *nam = 0 ;
  123. #ifndef VMS
  124.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  125.  
  126.       if (*file != '\0') {
  127.          *nam++ = DIRSEP;                  /* add separator */
  128.          (void)strcpy(nam,file);                   /* tack the file on */
  129.       }
  130.       else
  131.          *nam = '\0' ;
  132. #else
  133.       (void)strcpy(nam,file);                   /* tack the file on */
  134. #endif
  135.       /* belated check -- bah! */
  136.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  137.          error("! overran allocated storage in search()");
  138.  
  139. #ifdef DEBUG
  140.       if (dd(D_PATHS))
  141.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  142. #endif
  143.       if ((fd=fopen(fname,mode)) != NULL)
  144.          return(fd);
  145.  
  146.    /* skip over PATHSEP and try again */
  147.    } while (*(path++));
  148.  
  149.    return(NULL);
  150.  
  151. }               /* end search */
  152.  
  153. FILE *
  154. pksearch(path, file, mode, n, dpi)
  155.         char *path, *file, *mode ;
  156.     char *n ;
  157.     halfword dpi ;
  158. {
  159.    extern char *getenv(), *newstring() ;
  160.    register char *nam ;                 /* index into fname */
  161.    register FILE *fd ;                  /* file desc of file */
  162.    char fname[MAXPATHLEN] ;             /* to store file name */
  163.    static char *home = 0 ;              /* home is where the heart is */
  164.    for (nam=path; *nam; nam++)
  165.       if (*nam == '%')
  166.          break ;
  167.    if (*nam == 0)
  168.       return search(path, file, mode) ;
  169.    if (*file == DIRSEP) {               /* if full path name */
  170.       if ((fd=fopen(file,mode)) != NULL)
  171.          return(fd) ;
  172.       else
  173.          return(NULL) ;
  174.    }
  175. #ifdef MSDOS
  176.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  177.       if ((fd=fopen(file,mode)) != NULL)
  178.          return(fd) ;
  179.       else
  180.          return(NULL) ;
  181.    }
  182. #else
  183. #ifdef atarist
  184.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  185.       if ((fd=fopen(file,mode)) != NULL)
  186.          return(fd) ;
  187.       else
  188.          return(NULL) ;
  189.    }
  190. #endif
  191. #endif
  192.    do {
  193.       /* copy the current directory into fname */
  194.       nam = fname;
  195.       /* copy till PATHSEP */
  196.       if (*path == '~') {
  197.          char *p = nam ;
  198.          path++ ;
  199. #ifdef atarist
  200.          while (*path && *path != PATHSEP &&
  201.         *path != PATHSEP2 && *path != DIRSEP)
  202. #else
  203.          while (*path && *path != PATHSEP && *path != DIRSEP)
  204. #endif
  205.             *p++ = *path++ ;
  206.          *p = 0 ;
  207.          if (*nam == 0) {
  208.             if (home == 0) {
  209.                if (home = getenv("HOME"))
  210.                   home = newstring(home) ;
  211.                else
  212.                   home = "." ;
  213.             }
  214.             strcpy(fname, home) ;
  215.          } else {
  216. #ifdef MSDOS
  217.             error("! ~username in path???") ;
  218. #else
  219. #ifdef atarist
  220.             error("! ~username in path???") ;
  221. #else
  222. #ifdef VMS
  223.             error("! ~username in path???") ;
  224. #else
  225.             struct passwd *pw = getpwnam(fname) ;
  226.             if (pw)
  227.                strcpy(fname, pw->pw_dir) ;
  228.             else
  229.                error("no such user") ;
  230. #endif
  231. #endif
  232. #endif
  233.          }
  234.          nam = fname + strlen(fname) ;
  235.       }
  236.       /* copy till PATHSEP */
  237. #ifdef atarist
  238.       while (*path != PATHSEP && *path != PATHSEP2 && *path) {
  239. #else
  240.       while (*path != PATHSEP && *path) {
  241. #endif
  242.          if (*path == '%') {
  243.             path++ ;
  244.             switch(*path) {
  245.                case 'd': sprintf(nam, "%d", dpi) ; break ;
  246.                case 'f': strcpy(nam, n) ; break ;
  247.                case 'm': if (mfmode == 0)
  248.                             if (actualdpi == 300) mfmode = "imagen" ;
  249.                             else if (actualdpi == 400) mfmode = "nexthi" ;
  250.                             else if (actualdpi == 635) mfmode = "linolo" ;
  251.                             else if (actualdpi == 1270) mfmode = "linohi" ;
  252.                             else if (actualdpi == 2540) mfmode = "linosuper" ;
  253.                          if (mfmode == 0)
  254.                             error("! MF mode not set, but used in pk path") ;
  255.                          strcpy(nam, mfmode) ;
  256.                          break ;
  257.                case 'p': strcpy(nam, "pk") ; break ;
  258.                case '%': strcpy(nam, "%") ; break ;
  259.                default: error("! bad format character in pk path") ;
  260.             }
  261.             nam = fname + strlen(fname) ;
  262.             if (*path)
  263.                path++ ;
  264.          } else
  265.             *nam++ = *path++;
  266.       }
  267. #ifndef VMS
  268.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  269. #endif /* VMS */
  270.  
  271.       *nam = '\0' ;
  272.  
  273.       /* belated check -- bah! */
  274.       if (strlen(fname) + 1 > MAXPATHLEN)
  275.          error("! overran allocated storage in search()");
  276.  
  277. #ifdef DEBUG
  278.       if (dd(D_PATHS))
  279.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  280. #endif
  281.       if ((fd=fopen(fname,mode)) != NULL)
  282.          return(fd);
  283.  
  284.    /* skip ove